package AnuncioMVC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/* ivan a pasado por aqui
para cambiartela porque si */

import sun.swing.plaf.synth.SynthFileChooserUIImpl;


public class Anuncio {

	protected int id=0;
	protected String titulo;
	protected String texto;
	protected float precio;
	protected boolean vendido;
	
	protected void setID(int id){
		this.id=id;
	}
	public int getId() {
		return id;
	}
	
	public String getTitulo() {
		return titulo;
	}
	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}
	public String getTexto() {
		return texto;
	}
	public void setTexto(String texto) {
		this.texto = texto;
	}
	public float getPrecio() {
		return precio;
	}
	public void setPrecio(float precio) {
		this.precio = precio;
	}
	public boolean isVendido() {
		return vendido;
	}
	public void setVendido(boolean vendido) {
		this.vendido = vendido;
	}
	
	
	public boolean  getAnuncio(int id){
		boolean correcto=false;
		Statement stmt=null;
		ResultSet rs=null;
		Connection conn=null;
		long serialVersionUID = 1L;
		// JDBC driver name and database URL
	    String driver="com.mysql.jdbc.Driver";  
	    String db_url="jdbc:mysql://localhost/JAVA";

	    //  Database credentials
	    String User = "root";
	    String Pass = "";
	    try{
			 // Register JDBC driver
	    	Class.forName("com.mysql.jdbc.Driver");
	         // Open a connection
	     	conn = DriverManager.getConnection(db_url,User,Pass);
	     	// Execute SQL query
	         stmt = conn.createStatement();
	         String sql;
	         sql = "SELECT * FROM anuncios where id='" + id + "';";
	         rs = stmt.executeQuery(sql);

	         // Extract data from result set
	         if(rs.next()){
	            //Retrieve by column name
		         setID(rs.getInt("id"));
		         setTitulo(rs.getString("titulo"));
		         setTexto(rs.getString("texto"));
		         setPrecio(rs.getFloat("precio"));
		         setVendido(rs.getBoolean("vendido"));
		         correcto=true;
	           	 
	         } 
	         rs.close();
	         stmt.close();
	         conn.close();
	       
		}catch(SQLException se){
			
	          
		}catch(Exception e){
			
		}finally{
				try {
					
					if(stmt!=null){
						stmt.close();
					}
					
					if(conn!=null){
						conn.close();
					}
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
	    }
	    
	    return correcto;
	}
	
	public boolean saveAnuncio(){
		if(this.id!=0){
			//Actualizo
			return saveAnuncio(this.id,this.texto,this.titulo,this.precio,this.vendido);
		}else{
			//Creo el anuncio
		return saveAnuncio(this.texto,this.titulo,this.precio,this.vendido);
		}
	}
	
	public boolean saveAnuncio(String texto, String titulo, Float precio, boolean vendido){
		boolean correcto=false;
		Statement stmt=null;
		Connection conn=null;
		
		// JDBC driver name and database URL
	    String driver="com.mysql.jdbc.Driver";  
	    String db_url="jdbc:mysql://localhost/JAVA";

	    //  Database credentials
	    String User = "root";
	    String Pass = "";
	    try{
			 // Register JDBC driver
	    	Class.forName("com.mysql.jdbc.Driver");
	         // Open a connection
	     	conn = DriverManager.getConnection(db_url,User,Pass);
	     	// Execute SQL query
	         stmt = conn.createStatement();
	         String sql;
	         sql = "INSERT INTO anuncios VALUES (default,'" + titulo + "','" + texto +"',"+ precio +", "+ vendido + ");";
	         stmt.executeUpdate(sql);
		
			}catch(Exception ex){
					ex.printStackTrace();
				}
				    return true;
	}
	
	public boolean saveAnuncio(int id,String texto, String titulo, Float precio, boolean vendido){
		boolean correcto=false;
		Statement stmt=null;
		Connection conn=null;
		
		// JDBC driver name and database URL
	    String driver="com.mysql.jdbc.Driver";  
	    String db_url="jdbc:mysql://localhost/JAVA";

	    //  Database credentials
	    String User = "root";
	    String Pass = "";
	    try{
			 // Register JDBC driver
	    	Class.forName("com.mysql.jdbc.Driver");
	         // Open a connection
	     	conn = DriverManager.getConnection(db_url,User,Pass);
	     	// Execute SQL query
	         stmt = conn.createStatement();
	         String sql;
	         sql = "UPDATE anuncios SET titulo='"+ titulo + "',texto='" + texto +"',precio="+ precio +",vendido="+ vendido + " where id=" +id+";";
	         stmt.executeUpdate(sql);
		
			}catch(Exception ex){
					ex.printStackTrace();
				}
				    return true;
	}
	
	
	
	public boolean deleteAnuncio(int id){
		boolean correcto=false;
		Statement stmt=null;
		Connection conn=null;
		
		// JDBC driver name and database URL
	    String driver="com.mysql.jdbc.Driver";  
	    String db_url="jdbc:mysql://localhost/JAVA";

	    //  Database credentials
	    String User = "root";
	    String Pass = "";
	    try{
			 // Register JDBC driver
	    	Class.forName("com.mysql.jdbc.Driver");
	         // Open a connection
	     	conn = DriverManager.getConnection(db_url,User,Pass);
	     	// Execute SQL query
	         stmt = conn.createStatement();
	         String sql;
	         sql = "DELETE FROM anuncios where id="+ id + ";";
	         stmt.executeUpdate(sql);
		
			}catch(Exception ex){
					ex.printStackTrace();
				}
				    return true;
	}
	
	
	
	
	
	
	
	
	
}
